define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','/assets/addons/kuerp/layui/layui.js'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function () {
            var dateTypes = Config.date_type
            var querytype = 2
            var start_date = '';
            var end_date = '';
            
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                var form = layui.form;
                var defaultDate = '';
                dateTypes.forEach(function (item, index) {
                    if (item.checked == true) {
                        defaultDate = item.value.join(' - ')
                        start_date = item.value[0]
                        end_date = item.value[1]
                    }
                })
                getChart(querytype);
                //执行一个laydate实例
                var laydateInstance =laydate.render({
                    elem: '#purchase_time',
                    type: 'date',
                    range: true,
                    value: defaultDate,
                    done: function(value, date, endDate){
                        console.log(value); //得到日期生成的值，如：2017-08-18
                        console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                        console.log(endDate);
                        start_date = value.split(' - ')[0]
                        end_date = value.split(' - ')[1]
                        getChart(querytype);
                    }
                });
                form.on('select(date_type)', function(data){
                    setTimeout(function () {
                        var type = data.value;
                        defaultDate = dateTypes[type].value.join(' - ')
                        // 重新调用 render 方法更新日期选择器
                        laydateInstance = laydate.render({
                            elem: '#purchase_time',
                            type: 'date',
                            range: true,
                            value: defaultDate,
                            done: function(value, date, endDate) {
                                start_date = value.split(' - ')[0]
                                end_date = value.split(' - ')[1]
                                getChart(querytype);
                            }
                        });
                        start_date = dateTypes[type].value[0]
                        end_date = dateTypes[type].value[1]
                        getChart(querytype);
                    },0);
                  });
            })
            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    var myChart = Echarts.init(document.getElementById('echart'), 'walden');
                    myChart.resize();

                    var myCharts = Echarts.init(document.getElementById('echart2'), 'walden');
                    myCharts.resize();
                }, 0);
            });

            $(document).on("click", ".btn_chart", function () {
                querytype = $(this).data('id');
                $('.btn_chart').removeClass('active');
                $(this).addClass('active');
                getChart(querytype)
            });
            
            function getChart(types) {
                console.log('types',start_date,end_date)
                $.ajax({
                    type : 'POST',
                    url : 'kuerp/datainfo/index',
                    data:{
                        'types':types,
                        start_date,
                        end_date
                    },
                    success : function(res){
                        var statics = res.statics
                        if (types==2) {
                            $('#sales_num').text(statics.sale_number)
                            $('#sales_mon').text(statics.sale_money)
                            $('#customers_mon').text(statics.unback_money)
                            $('#goods_stock').text(statics.goods_stock)
                            $('#back_num').text(statics.back_num)
                            $('#back_mon').text(statics.back_money)
                        }
                        if(types<3){
                            $('#echart2').hide();$('#echart').show();
                            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
                            var option = {
                                title: {
                                    text: '',
                                    subtext: ''
                                },
                                color: [
                                    "#18d1b1",
                                    "#3fb1e3",
                                    "#626c91",
                                    "#a0a7e6",
                                    "#c4ebad",
                                    "#96dee8"
                                ],
                                tooltip: {
                                    trigger: 'axis'
                                },
                                legend: {
                                    data: [res.legend]
                                },
                                toolbox: {
                                    show: false,
                                    feature: {
                                        magicType: {show: true, type: ['stack', 'tiled']},
                                        saveAsImage: {show: true}
                                    }
                                },
                                xAxis: {
                                    type: 'category',
                                    boundaryGap: false,
                                    data: res.column
                                },
                                yAxis: {},
                                grid: [{
                                    left: 'left',
                                    top: 'top',
                                    right: '10',
                                    bottom: 30
                                }],
                                series: [{
                                    name: res.legend,
                                    type: 'bar',
                                    smooth: true,
                                    areaStyle: {
                                        normal: {}
                                    },
                                    lineStyle: {
                                        normal: {
                                            width: 1.5
                                        }
                                    },
                                    data: res.data
                                }]
                            };
                            // 使用刚指定的配置项和数据显示图表。
                            myChart.setOption(option);
                            window.addEventListener("resize", function () {
                                myChart.resize();
                            });
                        }else{
                            $('#echart').hide();$('#echart2').show();
                            var myChart = Echarts.init(document.getElementById('echart2'), 'walden');
                            option = {
                                title: {
                                    text: res.title,
                                    left: 'center'
                                },
                                tooltip: {
                                    trigger: 'item'
                                },
                                legend: {
                                    orient: 'vertical',
                                    left: 'left'
                                },
                                series: [
                                    {
                                        name: res.legend,
                                        type: 'pie',
                                        radius: '50%',
                                        data: res.data,
                                        emphasis: {
                                            itemStyle: {
                                                shadowBlur: 10,
                                                shadowOffsetX: 0,
                                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                                            }
                                        }
                                    }
                                ]
                            };
                            // 使用刚指定的配置项和数据显示图表。
                            myChart.setOption(option);
                            window.addEventListener("resize", function () {
                                myChart.resize();
                            });
                        }
                    }
                });
            }
        },
        stockworing: function () {

            layui.use(['table','upload'], function(){
                var table = layui.table;

                var storesList = Config.storesList;
                var stores = xmSelect.render({
                    el: '#stores',
                    language: 'zn',
                    tips: '仓库',
                    data: storesList
                });

                var goodsList = Config.goodsList;
                var goodstype = xmSelect.render({
                    el: '#goods',
                    language: 'zn',
                    tips: '商品分类',
                    data: goodsList
                });

                table.render({
                    elem: '#table'
                    ,url:'kuerp/datainfo/stockworing'
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.rows //解析数据列表
                        };
                    }
                    ,toolbar: '#thisToolbar' //开启头部工具栏，并为其绑定左侧模板
                    ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,cols: [[
                        {field: 'goods_type_id', title: __('商品分类'),templet: function (d) {
                                if(d.goodstype.supplier_type_name!=undefined){
                                    return d.goodstype.supplier_type_name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'goods_code', title: __('商品编号')},
                        {field: 'goods_name', title: __('商品名称')},
                        {field: 'goods_barcode', title: __('商品条码')},
                        {field: 'goods_image', title: __('商品图片'), operate: false,templet: function (d) {
                                return "<a href='javascript:;'><img src="+d.goods_image+"></a>";
                            }},
                        {field: 'unit', title: __('单位'),templet: function (d) {
                                if(d.more_unit==1){
                                    return d.unitconversion;
                                }
                                if(d.more_unit==2){
                                    return d.unit.name;
                                }
                            }},
                        {field: 'stores', title: __('仓库'),templet: function (d) {
                                if(d.stores.name!=undefined){
                                    return d.stores.name;
                                }else{
                                    return '';
                                }
                            }},
                        {field: 'min_stock', title: __('最低库存')},
                        {field: 'max_stock', title: __('最高库存')},
                        {field: 'stock', title: __('当前库存')},
                    ]]
                });

                //头工具栏事件
                table.on('toolbar(table)', function(obj){
                    switch(obj.event){
                        case 'refresh':
                            table.reload('table', []);
                            break;
                    }
                });
                //搜索事件
                $('#search').on('click', function () {
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(getSearch()),
                            op:JSON.stringify(getOp()),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                });
                function getSearch() {
                    var search = {};

                    var storesArr = stores.getValue();
                    var stores_id = '';
                    if(storesArr.length>0){
                        $(storesArr).each(function (k,v) {
                            if(stores_id===''){
                                stores_id += v.value;
                            }else{
                                stores_id += ','+v.value;
                            }
                        })
                    }
                    search.stores_id = stores_id;

                    var typeArr = goodstype.getValue();
                    var type_id = '';
                    if(typeArr.length>0){
                        $(typeArr).each(function (k,v) {
                            if(type_id===''){
                                type_id += v.value;
                            }else{
                                type_id += ','+v.value;
                            }
                        })
                    }
                    search.type_id = type_id;

                    var goods_code = $('#goods_code').val();
                    if(goods_code!=''){
                        search.goods_code = goods_code;
                    }
                    var goods_name = $('#goods_name').val();
                    if(goods_name!=''){
                        search.goods_name = goods_name;
                    }
                    return search;
                }
                function getOp() {
                    var op = {};
                    op.goods_code = 'LIKE';
                    op.goods_name = 'LIKE';
                    return op;
                }
            });
        },
    };

    return Controller;
});
